<?php
Yii::import('application.extensions.yii-mail.*');
class JLMailer extends YiiMail {
	public function sendMailWithTemplate($email = NULL, $subject = NULL, $view = NULL, $data = NULL,$from=NULL) { // add argument {from} , author: thinhpq
		if (isset($email) && isset($view)) {
			/**
			 * @todo Change message email
			 */
			$subject = isset($subject) ? $subject : "(No Subject)";

			try {
				$message = new YiiMailMessage;
				$message->view = $view;
				$message->setSubject($subject);
				
				$message->setBody(
					array(
						'data'	=> isset($data) ? $data : array()
					),
					'text/html'
				);

				$message->addTo($email);
				$from = Yii::app()->params['adminEmail'];
				
				// $message->from = $from;
				$message->from = array($from => "Orange Offer");

				Yii::app()->mail->send($message);
				return array(
					'error'	=> false,
					'data'=>$message
				);
			} catch (Exception $ex) {
				// TODO : Can't send mail
				
				return array(
					'error'	=> true,
					'message'=> $ex->getMessage()
				);
			}
		}
	}
	
	public function sendMail($alias = NULL, $email = NULL, $data = array()) {
		if (!isset($alias) || empty($alias) || empty($email) || empty($data) ) {
			return false;
		}
		
// 		unset(Yii::app()->session['modelEmailTemplate']);

		/**
		 * Thinhpq hot fix: I remove session email template because you has query DB 
		 * Thinhpq add code 6/4/2013
		 **/
		
		if(!empty(Yii::app()->session['modelEmailTemplate'])){
			unset(Yii::app()->session['modelEmailTemplate']);
		}
		Yii::import('application.modules.admin_manage.models.EmailTemplate');
		
		$isLayout = 0;
		
		if(isset(Yii::app()->session['modelEmailTemplate']) && !empty(Yii::app()->session['modelEmailTemplate']) && (Yii::app()->session['modelEmailTemplate']['alias'] == $alias)) {
			$emailTemplate = Yii::app()->session['modelEmailTemplate'];
		} else {
			$modelEmailTemplate = EmailTemplate::model()->findByAttributes(array(
				'alias'	=> $alias
			));
			

			if (!empty($modelEmailTemplate)) {
				$emailTemplate = $modelEmailTemplate->attributes;
				$isLayout = $modelEmailTemplate->layout;
				Yii::app()->session['modelEmailTemplate'] = $emailTemplate;
			}
		}
		
		if (empty($emailTemplate)) {
			return array(
				'error'	=> true,
				'message'=> 'Alias email template is invalid'
			);
		}
		
		$view = 'email_template';
		if($isLayout==1) $view = 'email_template_none_layout';
		
		
		$subject = (!empty($emailTemplate['subject'])) ? $emailTemplate['subject'] : null;
		$title = (!empty($emailTemplate['title'])) ? $emailTemplate['title'] : null;
		$footer = (!empty($emailTemplate['footer'])) ? $emailTemplate['footer'] : null;
		$content = (!empty($emailTemplate['content'])) ? $emailTemplate['content'] : null;
		
		foreach($data as $key=>$value) {
			$title = str_replace("{%".$key."%}", $data[$key], $title);
			$subject = str_replace("{%".$key."%}", $data[$key], $subject);
			$content = str_replace("{%".$key."%}", $data[$key], $content);
		}
		
		$data = array(
			'title'		=> $title,
			'content'	=> $content,
			'footer'	=> $footer
		);

		return JLMailer::sendMailWithTemplate($email, $subject, $view, $data);
	}
}
